package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.bean.ClassMate;
import com.example.myapplication.utli.UtilsEnum;

import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "RegisterActivity";
    EditText mEdName, mEdEmail, mEdPhone, mEdPassword, mEdConfirmPassword;
    Button mBtnRegister;
    Spinner mSpinnerDept, mSpinnerMajor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initData();
        initEvent();
    }

    void initView() {
        mEdName = findViewById(R.id.ed_name_register);
        //mEdEmail,mEdPhone,mEdPassword,mEdConfirmPassword;
        mEdEmail = findViewById(R.id.ed_email_register);
        mEdPhone = findViewById(R.id.ed_phone_register);
        mEdPassword = findViewById(R.id.ed_password_register);
        mEdConfirmPassword = findViewById(R.id.ed_confirm_password_register);
        mBtnRegister = findViewById(R.id.btn_register);
        mSpinnerDept = findViewById(R.id.spinner_dept_register);
        mSpinnerMajor = findViewById(R.id.spinner_major_register);
    }

    void initEvent() {
        mBtnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pwd = mEdPassword.getText() + "";
                String confirmPwd = mEdConfirmPassword.getText() + "";
                String name = mEdName.getText() + "";
                String email = mEdEmail.getText() + "";
                String phone = mEdPhone.getText() + "";
                String dept = mSpinnerDept.getSelectedItem().toString();
                String major = mSpinnerMajor.getSelectedItem().toString();
                Log.e(TAG, "onClick: " + dept);
                if (!pwd.isEmpty() && pwd.equals(confirmPwd) && !name.isEmpty() && !email.isEmpty() && !phone.isEmpty()) {
                    ClassMate.DataDTO user = new ClassMate.DataDTO();
                    user.setName(name);
                    user.setTelephone(phone);
                    user.setMajor(major);
                    user.setDepartment(dept);
                    user.setPassword(pwd);
                    user.setRole("学生");
                    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
                    JsonObjectRequest request = new JsonObjectRequest(
                            Request.Method.POST, UtilsEnum.URL.url + "user/register?name=" +
                            name + "&password=" +
                            pwd + "&email=" + email + "&telephone=" + phone + "&department=" + dept + "&major=" + major + "&role=学生"
                            , new JSONObject(), res -> {
                        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                        startActivity(intent);
                        }, error -> {
                    }
                    );
                    requestQueue.add(request);
                } else {
                    Context context = getApplicationContext();
                    Toast toast = Toast.makeText(context, "两次密码必须一致,且其它不能为空", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });
    }

    void initData() {
    }
}